<template>
  <!-- 主体区域 -->
  <section id="app">
    <TodoHeader @add='addFn'></TodoHeader>
    <TodoMain @del='delFn' :list='list'></TodoMain>
    <TodoFooter @clear='clearFn' :list='list'></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from'./components/TodoHeader.vue'
import TodoMain from'./components/TodoMain.vue'
import TodoFooter from'./components/TodoFooter.vue'
export default {
  data () {
    return {
      list:JSON.parse(localStorage.getItem('list'))||[
        {id:1,name:'跑步'},
        {id:2,name:'游泳'},
        {id:3,name:'蛙跳'}
      ]
    }
  },
  methods:{
    addFn(todoName){
      // console.log(todoName);
      this.list.unshift(
        {id:+new Date(),
        name:todoName}
      )
    },
    delFn(id){
      // console.log('父级',id);
      this.list=this.list.filter(item=>item.id!==id)
    },
    clearFn(){
      this.list=[]
    }
  },
  watch:{
    list:{
      deep:true,
      handler(newValue){
        localStorage.setItem('list',JSON.stringify(newValue))
      }
    }
  },
  components:{
    TodoFooter,
    TodoHeader,
    TodoMain
  }
}
</script>

<style>

</style>
